package com.school.controller;

import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisOptions;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisPrompt;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

@RestController
public class AudioModelController {
    @Autowired
    private DashScopeSpeechSynthesisModel speechSynthesisModel;
    private static final String TEXT ="臭傻呗马文超，臭傻呗马文超，臭傻呗马文超，臭傻呗马文超";
    private static final String FILE_PATH = "D:\\zhongruan1\\springaidemo\\src\\main\\resources" ;

    @GetMapping( "/tts")
    public void tts() throws IOException {
//使用构建器模式创建DashScopeSpeechSynthesisoptions 实例并设置参数
        DashScopeSpeechSynthesisOptions options = DashScopeSpeechSynthesisOptions.builder()
                .withSpeed(1.0)
//设置语速
                .withPitch(0.9)
//设置音调
                .withVolume(60)
//设置音量
                .build();
        SpeechSynthesisResponse response = speechSynthesisModel.call(new SpeechSynthesisPrompt(TEXT, options));
        File file = new File(  FILE_PATH + "/output.mp3");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            ByteBuffer byteBuffer = response.getResult().getOutput().getAudio();
            fos.write(byteBuffer.array());
        }catch (IOException e) {
            throw new IOException(e.getMessage());
        }
    }
}